package com.qing.dish;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo3 {


    private static List<Dish> menu= Arrays.asList(
            new Dish("黄焖鸡米饭",false,15,"肉"),
            new Dish("水煮鱼",true,12,"鱼"),
            new Dish("酸菜鱼",false,20,"鱼"),
            new Dish("脆皮鸡米饭",false,18,"米饭"),
            new Dish("麻辣香锅",true,32,"米饭"),
            new Dish("鸡公煲",false,20,"肉"),
            new Dish("汉堡王",true,100,"米饭"),
            new Dish("至尊牛排",false,20,"肉"),
            new Dish("蛋炒饭",true,12,"素"),
            new Dish("咖喱",false,29,"米饭"),
            new Dish("炒白菜",false,8,"素"),
            new Dish("炒萝卜",false,9,"素"),
            new Dish("烤肉",true,50,"肉")
    );
    public static void main(String[] args) {
        //求和有 封箱拆箱操作
        menu.stream().map(Dish::getCalories).reduce(Integer::sum);
        /**
         * 请注意，如果流是空的，sum则默认返回0。
         * IntStream还支持其他的方便方法，如max、min、average等。
         */
       menu.stream().mapToInt(Dish::getCalories).sum();

       IntStream intStream=menu.stream().mapToInt(Dish::getCalories);
       Stream<Integer> stream=intStream.boxed();

        Integer max=menu.stream().mapToInt(Dish::getCalories).max().orElse(1);

        IntStream.range(1,10).forEach(System.out::print); //123456789
        System.out.println("");
        System.out.println("-----------------");
        IntStream.rangeClosed(1,10).forEach(System.out::print); //12345678910

        System.out.println("");
        System.out.println("-----------------");
        Stream<int[]> py=IntStream.rangeClosed(1,100).boxed().flatMap(
                a->IntStream.rangeClosed(a,100).
                        filter(b->Math.sqrt(a*a+b*b)%1==0)
                        .mapToObj(b->new int[]{a,b,(int)Math.sqrt(a*a+b*b)})
        );
        py.forEach(t-> System.out.println(t[0]+","+t[1]+","+t[2]));
    }
}
